
import { TestConfig } from '../common/testConfig.js';
import {
    deletePeriodSettleNetworkCache, getPeriodSettleData,
    settleCombineOrder,removeSettleCombineOrder
} from '../../../public/network/api'
import { CommunicationPageData, ApiReturnStruct, NetworkParser, DataPage } from '../../network/communicationConverter.js';
import { getApiData } from '../../network/communicationConverter.js';
import { TestSimulateData } from '../testMgr/testSimulateData.js';
import { PeriodSettlementOrder } from '../periodSettlement/periodSettlementOrder.js';
import { Engine } from '../../engine/engine.js';
import { MessageUtil } from '@/utils/MessageUtil.js';

export class PeriodSettlementUiAction {
    /**
     * 获取资金账户列表数据
     * @param {Object} options
     * @param {string} options.orgId - Organization ID
     * @param {DataPage} options.pageInfo - Pagination information
     * @param {Object} [options.filter] - PeriodSettlementFilter
     * @returns {Promise<ApiReturnStruct>}
     */
    async getPeriodSettlementData({ orgId, pageInfo, filter = null }) {
        if (TestConfig.periodSettlementTest) {
            // 模拟数据
            let simData = new CommunicationPageData();
            simData.dataList = new TestSimulateData().simPeriodSettlementOrder();

            let ret = new ApiReturnStruct();
            ret.data = simData;
            return ret;
        } else {
            const apiData = await getApiData(
                PeriodSettlementOrder.fromJson,
                orgId || Engine.profile.org.orgId,
                pageInfo,
                getPeriodSettleData,
                filter == null ? {} : filter.toJson(),
                true // showLoadingPage - defaulting to true as it's not specified
            );
            return apiData;
        }
    }

    /**
     * 发起混合单据期末结算
     * @param {Array<string>} ids - 加工单ID列表
     * @param {string} orgId - 组织ID
     * @returns {Promise<any>}
     */
    async settleCombineOrder(settleOrder, orgId = null) {
        try {
            orgId = orgId ?? Engine.profile.org.orgId;

            const res = await settleCombineOrder({
                settleOrder: settleOrder,
                orgId: orgId
            });

            // 解析数据
            const apiResult = NetworkParser.parseNetworkResult(res, null);

            if (apiResult.resErr != null && typeof apiResult.resErr === 'string') {
                MessageUtil.showErrorMsg("发起结算失败:" + apiResult.resErr);
                return null;
            }

            return apiResult;
        } catch (e) {
            MessageUtil.showErrorMsg("发起结算失败:" + e.message);
            return null;
        }
    }

        /**
     * 发起混合单据期末结算
     * @param {Array<string>} ids - 加工单ID列表
     * @param {string} orgId - 组织ID
     * @returns {Promise<any>}
     */
    async removeSettleCombineOrder(settleOrder, orgId = null) {
        try {
            orgId = orgId ?? Engine.profile.org.orgId;

            const res = await removeSettleCombineOrder({
                settleOrder: settleOrder,
                orgId: orgId
            });

            // 解析数据
            const apiResult = NetworkParser.parseNetworkResult(res, null);

            if (apiResult.resErr != null && typeof apiResult.resErr === 'string') {
                MessageUtil.showErrorMsg("删除结算失败:" + apiResult.resErr);
                return null;
            }

            return apiResult;
        } catch (e) {
            MessageUtil.showErrorMsg("删除结算失败:" + e.message);
            return null;
        }
    }
}